<?php
namespace App\Common\Tencent\Cos;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Auth;
use Qcloud\Cos\Client;

class CosObject {

    protected $SecretId;
    protected $SecretKey;
    protected $AppId;
    protected $Bucket;
    //地区
    protected $Region;
    protected $Schema;
    protected $User;
    protected static $cosClient;

    public function __construct($Bucket = '',$Region = "ap-beijing",$Schema = "https")
    {
        $config = Config::get('TencentConfig.cos');
        $this->SecretId = $config['SecretId'];
        $this->SecretKey = $config['SecretKey'];
        $this->AppId = $config['AppId'];
        $this->Bucket = $Bucket.'-'.$this->AppId;
        $this->Region = $Region;
        $this->Schema = $Schema;
        $this->User = Auth::user();
    }

    public function setBucket($Bucket){
        $this->Bucket = $Bucket;
    }

    /**
     * 初始话
     */
    private function init(){
        $cosConfig = [
            'region' => $this->Region,
            'schema' => $this->Schema, //协议头部，默认为http
            'credentials'=>[
                'secretId'  =>  $this->SecretId,
                'secretKey' => $this->SecretKey,
            ]
        ];
        self::$cosClient = new Client($cosConfig);
    }

    //简单上传
    public function putObject($body , $key, $options = [])
    {
        $this->init();
        try {
            $result = $this::$cosClient->upload(
                $Bucket = $this->Bucket, //格式：BucketName-APPID
                $Key = $key,
                $Body = $body,
                $options
            );
            // 请求成功
            return $result;
        } catch (\Exception $e) {
            // 请求失败
            return $e->getMessage();
        }
    }

    //列表
    public function listObjects()
    {
        $this->init();
        try {
            $result = $this::$cosClient->listObjects(array(
                'Bucket' => $this->Bucket, //格式：BucketName-APPID
                'EncodingType' => 'url',
                //'Prefix' => 'user_id_7/long',
                //'Delimiter' => '',
                'MaxKeys' => 50,
            ));
            // 请求成功
            $url_arr = [];
            foreach ($result['Contents'] as $name_kes){
                $signedUrl = $this::$cosClient->getObjectUrl($this->Bucket, $name_kes['Key'], '+30 minutes'); //签名的有效时间
                array_push($url_arr,$signedUrl);
            }
            return $url_arr;
        } catch (\Exception $e) {
            // 请求失败
            return $e->getMessage();
        }
    }

    public function delObjects($key,$version = '')
    {
        $this->init();
        try {
            $result = $this::$cosClient->deleteObject(array(
                'Bucket' => $this->Bucket, //格式：BucketName-APPID
                'Key' => $key,
                'VersionId' => $version
            ));
            // 请求成功
            return $result;
        } catch (\Exception $e) {
            // 请求失败
            return $e->getMessage();
        }
    }

    /**
     * 获取临时地址
     * @param $key
     * @return string
     */
    public function getObjectUrl($key,$time = 60)
    {
        $this->init();
        try {
            $signedUrl = $this::$cosClient->getObjectUrl($this->Bucket, $key, '+'.$time.' minutes'); //签名的有效时间
            return $signedUrl;
        } catch (\Exception $e) {
            // 请求失败
            return $e->getMessage();
        }
    }
}
